export const token = getCookie('token');

export function createReducer(initialState, reducerMap) {
  return (state = initialState, action) => {
    const reducer = reducerMap[action.type];

    return reducer
      ? reducer(state, action.payload)
      : state;
  };
}

export function parseJSON(response) {
  return response.json()
}

export function createConstants(...constants) {
  return constants.reduce((acc, constant) => {
    acc[constant] = constant;
    return acc;
  }, {});
}

/**
 * 获取时间差
 * 
 */
export function getTimeCha(end){

  var t1 = end;
  var d1 = t1.replace(/\-/g, "/");
  var date1= new Date();  //开始时间  
  var date2 = new Date(d1); //结束时间
  var date3 =  new Date(date2).getTime() - date1.getTime();   //时间差的毫秒数        

  //------------------------------  

  //计算出相差天数  
  var days=Math.floor(date3/(24*3600*1000))  

  //计算出小时数  

  var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数  
  var hours=Math.floor(leave1/(3600*1000))  
  //计算相差分钟数  
  var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数  
  var minutes=Math.floor(leave2/(60*1000))  
  //计算相差秒数  
  var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数  
  var seconds=Math.round(leave3/1000) 
  return  `${days}天${hours}小时${minutes}分${seconds}秒`;
}


/**
 * 设置cookie
 * @param {*cookie名称} c_name 
 * @param {*cookie值} value 
 * @param {*过期时间，以天为单位} expiredays 
 */
export function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

export function getCookie(name){
  var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
  if(arr=document.cookie.match(reg))
  return unescape(arr[2]);
  else
  return null;
}

/**
 * 时间戳转 yyyy-MM-dd HH-mm-ss
 * @param {*} inputTime 
 */
export function formatDateTime(inputTime) {    
  if(!inputTime){
    return '';
  }
  var date = new Date(inputTime);  
  var y = date.getFullYear();    
  var m = date.getMonth() + 1;    
  m = m < 10 ? ('0' + m) : m;    
  var d = date.getDate();    
  d = d < 10 ? ('0' + d) : d;    
  var h = date.getHours();  
  h = h < 10 ? ('0' + h) : h;  
  var minute = date.getMinutes();  
  var second = date.getSeconds();  
  minute = minute < 10 ? ('0' + minute) : minute;    
  second = second < 10 ? ('0' + second) : second;   
  return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;    
};  